/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
package de.thwildau.tm.moses.examples.nmea.app;

import java.util.List;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;

import de.thwildau.tm.moses.netty.channel.rxtx.CommPortUtil;
import de.thwildau.tm.moses.netty.channel.rxtx.RXTXChannelFactory;
import de.thwildau.tm.moses.netty.channel.rxtx.RXTXDeviceAddress;

/**
 * Connector class to connect to a specific serial port. The given {@link NmeaMessageListener} is used to
 * receive the NMEA messages that were read.
 * 
 * @author Thomas Kistel
 * @version $Revision: 764 $
 */
public class SerialPortConnecter {

    private boolean exit = true;
    private Channel channel;
    private ClientBootstrap bootstrap;
    private NmeaMessageListener mainFrame;


    public SerialPortConnecter(NmeaMessageListener mainFrame) {
        this.mainFrame = mainFrame;
    }

    public boolean isExit() {
        return exit;
    }

    public void setExit(boolean exit) {
        this.exit = exit;
    }

    public void connect(String port) {
        String deviceAddress = port;

        bootstrap = new ClientBootstrap(new RXTXChannelFactory(Executors.newCachedThreadPool()));
        // Configure the event pipeline factory.
        bootstrap.setPipelineFactory(new NmeaPipelineFactory(mainFrame));

        // Make a new connection.
        ChannelFuture connectFuture = bootstrap.connect(new RXTXDeviceAddress(deviceAddress));

        // Wait until the connection is made successfully.
        channel = connectFuture.awaitUninterruptibly().getChannel();

        setExit(false);
    }

    public List<String> getSerialCommPortIdentifiers() {
        return CommPortUtil.getSerialCommPortIdentifiers();
    }

    public void disconnect() {
        // Close the connection.
        channel.close().awaitUninterruptibly();

        // Shut down all thread pools to exit.
        bootstrap.releaseExternalResources();

        setExit(true);
    }

}
